/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using EvaluationException = System.Qizx.Api.EvaluationException;
using System.Qizx.XQuery;
using Expression = System.Qizx.XQuery.Op.Expression;
namespace System.Qizx.XQuery.Dt
{
	
	public class DoubleType:NumericType
	{
		public override string ShortName
		{
			get
			{
				return "double";
			}
			
		}
		
		public override int quickCode()
		{
			return QT_DOUBLE;
		}
		
		// public boolean accepts( XQType that ) {
		// // attention: accept all numeric types
		// // It is NOT equivalent to super.accepts() !
		// return XQItemType.isNumeric(that.getItemType());
		// }
		
		public override bool encompasses(XQItemType valueType)
		{
			// encompasses all numeric types:
			return XQType.NUMERIC.encompasses(valueType);
		}
		
		public override bool promotable(XQItemType type)
		{
			return (type == UNTYPED_ATOMIC || type == FLOAT || type.IsSubTypeOf(DECIMAL));
		}
		
		public override XQValue cast(XQItem value_Renamed, EvalContext context)
		{
			try
			{
				XQItemType type = value_Renamed.ItemType;
				if (type is BinaryType || type is QNameType || type is AnyURIType)
					invalidCast(type);
				if (context.sObs() && (type is MomentType || type is DurationType))
					invalidCast(type);
				return new SingleDouble(value_Renamed.ValueAsDouble);
			}
			catch (EvaluationException e)
			{
				castException(e);
				return null; // dummy
			}
		}
		
		public override XQValue convertFromObject(System.Object object_Renamed)
		{
			double value_Renamed = 0;
			if (object_Renamed is System.Double)
				value_Renamed = ((System.Double) object_Renamed);
			else if (object_Renamed is System.Int64)
				value_Renamed = (double) ((System.Int64) object_Renamed);
			else if (object_Renamed is System.Int32)
				value_Renamed = (double) ((System.Int32) object_Renamed);
			else if (object_Renamed is System.Int16)
				value_Renamed = (double) ((System.Int16) object_Renamed);
			else if (object_Renamed is System.SByte)
				value_Renamed = (double) ((System.SByte) object_Renamed);
			else
				invalidCast(this);
			return new SingleDouble(value_Renamed);
		}
		
		public override System.Object convertToObject(Expression expr, Focus focus, EvalContext context)
		{
			return (double) expr.evalAsDouble(focus, context);
		}
		
		public override XQValue convertFromArray(System.Object object_Renamed)
		{
			double[] result = (double[]) object_Renamed;
			return new DoubleArraySequence(result, result.Length);
		}
		
		public override System.Object convertToArray(XQValue value_Renamed)
		{
			return DoubleArraySequence.expandDoubles(value_Renamed);
		}
	}
}